/**************************************************************************
 * @name:  004_计数排序.c
 * @brief: 这是计数排序代码案例
 * @date:  2025/01/02
 * @author: boluzs@163.com
 * @note: None
 * @version: 1.0.0
 * @Copyright (c) 2025 by boluzs@163.com, All Rights Reserved.
 **************************************************************************/
#include <stdio.h>
#include <stdlib.h>

void CountSort(int *arr, int *buf, int size)
{
    int i = 0, j = 0;
    for (i = 0; i < size; i++)
    {
        int cnt = 0;
        for (j = 0; j < size; j++)
        {
            // 需要用最小值元素和序列中的其他元素比较
            if (arr[j] < arr[i])
            {
                cnt++;
            }
        }
        printf("cnt:%d----i:%d---num:%d\n ", cnt, i, arr[i]);
        buf[cnt] = arr[i];
    }
}

int main()
{
    int *buf = (int *)calloc(1, sizeof(int) * 8);
    int *arr = (int *)calloc(1, sizeof(int) * 8);
    arr[0] = 3;
    arr[1] = 6;
    arr[2] = 2;
    arr[3] = 1;
    arr[4] = 8;
    arr[5] = 7;
    arr[6] = 5;
    arr[7] = 4;
    CountSort(arr, buf, 8);
    for (int i = 0; i < 8; i++)
    {
        printf("%d ", buf[i]);
    }
    printf("\n");
    free(arr);
    free(buf);
    return 0;
}